﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace NewsReader
{
    public class DataProvider
    {
        public static List<RssItem> GetRssItems(Stream stream)
        {
            XDocument doc = XDocument.Load(stream);//"SampleData/TopHeadlines.xml");
            var channel = doc.Root.Element("channel");
            var items = channel.Elements("item");
            List<RssItem> rssItems = items.Select(item => new RssItem()
            {
                Title = item.Element("title").Value,
                Description = item.Element("description").Value,
                PublishedDate = item.Element("pubDate").Value,
                Url = item.Element("link").Value,

            }).ToList();
            return rssItems;

        }
        public static List<RssProvider> GetRssProviders()
        {
            XDocument providerXml = XDocument.Load("Data/Providers.xml");
            var providers = providerXml.Root.Elements();

            return providers.Select(provider => new RssProvider()
            {
                Name = provider.Attribute("Name").Value,
                Code = provider.Attribute("Code").Value,
                FeedUrls = _getProviderFeeds(provider)
            }).ToList();
        }

        private static Dictionary<string, string> _getProviderFeeds(XElement provider)
        {
            return provider.Elements().ToDictionary(x => x.Attribute("Name").Value, x => x.Attribute("Url").Value);
        }

    }

}

